package com.wswy.wzcx.ui.web;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.tencent.smtt.sdk.WebView;
import java.util.Map;

public class BaseWebView extends WebView {

  //https://juejin.im/post/5a94f9d15188257a63113a74

  private boolean mTouchByUser;

  public BaseWebView(Context context) {
    super(context);
  }

  public BaseWebView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
  }

  public BaseWebView(Context context, AttributeSet attributeSet, int i) {
    super(context, attributeSet, i);
  }

  public BaseWebView(Context context, AttributeSet attributeSet, int i, boolean b) {
    super(context, attributeSet, i, b);
  }

  public BaseWebView(Context context, AttributeSet attributeSet, int i,
      Map<String, Object> map, boolean b) {
    super(context, attributeSet, i, map, b);

  }

  @Override
  public final void loadUrl(String url, Map<String, String> additionalHttpHeaders) {
    super.loadUrl(url, additionalHttpHeaders);
    resetAllStateInternal(url);
  }

  @Override
  public void loadUrl(String url) {
    super.loadUrl(url);
    resetAllStateInternal(url);
  }

  @Override
  public final void postUrl(String url, byte[] postData) {
    super.postUrl(url, postData);
    resetAllStateInternal(url);
  }

  @Override
  public final void loadData(String data, String mimeType, String encoding) {
    super.loadData(data, mimeType, encoding);
    resetAllStateInternal(getUrl());
  }

  @Override
  public final void loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding,
      String historyUrl) {
    super.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
    resetAllStateInternal(getUrl());
  }

  @Override
  public void reload() {
    super.reload();
    resetAllStateInternal(getUrl());
  }





  public boolean isTouchByUser() {
    return mTouchByUser;
  }

  private void resetAllStateInternal(String url) {
    if (!TextUtils.isEmpty(url) && url.startsWith("javascript:")) {
      return;
    }
    resetAllState();
  }

  // 加载url时重置touch状态
  protected void resetAllState() {
    mTouchByUser = false;
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    switch (event.getActionMasked()) {
      case MotionEvent.ACTION_DOWN:
        //用户按下到下一个链接加载之前，置为true
        mTouchByUser = true;
        break;
    }
    return super.onTouchEvent(event);
  }



}
